<template>
  <div class="theme-picker">
    <!-- <el-color-picker v-model="theme"
                     :predefine="['#F2FFEE', '#409eff', '#67c23a','#909399','#e6a23c', '#f56c6c', '#f5222d' ]"
                     class="theme-picker"
    popper-class="theme-picker-dropdown" />-->
    <div>
      <i :style="{color:defaultTheme}"
         class="el-icon-user-solid"
         @click="open"></i>
    </div>
    <div class="theme-content"
         v-if="isOpenSetting">
      <div class="flex justify-start align-center">
        <div :class="selectColor===item?'color-item active':'color-item'"
             v-for="(item,index) in colorList"
             :key="index"
             :style="{background:item}"
             @click="selectChang(item)"></div>
      </div>
      <div class="flex justify-between align-center margin-top">
        <el-input v-model="selectColor"></el-input>
        <el-button @click="selectTheme"
                   size="small">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import Theme from '@theme'
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      submitSate: false,
      chalk: '', // content of theme-chalk css
      theme: '', // '#F2FFEE',
      colorList: ['#409eff', '#5AB53C', '#67c23a', '#909399', '#e6a23c'],
      selectColor: ''
    }
  },
  computed: {
    isOpenSetting() {
      return this.$store.state.settings.isOpenSetting
    },
    defaultTheme() {
      const defaultTheme = this.$store.state.settings.theme
      // eslint-disable-next-line vue/no-side-effects-in-computed-properties
      this.selectColor = defaultTheme
      return defaultTheme
    }
  },
  watch: {
    defaultTheme: {
      handler: function (val, oldVal) {
        this.theme = val
        this.selectColor = val
      },
      immediate: true
    },
    async theme(val) {
      if (val) {
        const thmeObj = new Theme(val)
        this.selectColor = val
        this.$emit('change', val)
      }
    }
  },
  methods: {
    ...mapActions([
      'settings/setOpenSetting'
    ]),
    selectChang(item) {
      this.selectColor = item
    },
    selectTheme() {
      this.theme = this.selectColor
      this['settings/setOpenSetting'](false)
    },
    open() {
      this['settings/setOpenSetting'](!this.isOpenSetting)
    }
  }

}
</script>
<style lang="scss">
.theme-picker {
  .el-icon-user-solid {
    font-size: 26px;
    cursor: pointer;
  }
  .theme-content {
    background-color: #fff;
    z-index: 999;
    position: absolute;
    top: 100px;
    right: 0px;
    width: 260px;
    box-sizing: border-box;
    padding: 40px 10px;
    border: 1px solid transparent;
    border-top-color: $gray-bg;
  }
  .color-item {
    width: 30px;
    height: 30px;
    border-radius: 5px;
    margin-right: 5px;
    cursor: pointer;
    &:hover {
      box-shadow: 0 0 3px 2px #409eff;
    }
    &.active {
      box-shadow: 0 0 3px 2px #409eff;
    }
  }
  .select-color {
    border: 1px solid #dcdfe6;
  }
  .el-input__inner {
    width: 160px;
    height: 35px;
    line-height: 35px;
  }
}
</style>
<style>
.theme-message,
.theme-picker-dropdown {
  z-index: 99999 !important;
}

.theme-picker .el-color-picker__trigger {
  height: 26px !important;
  width: 26px !important;
  padding: 2px;
}

.theme-picker-dropdown .el-color-dropdown__link-btn {
  display: none;
}
</style>
